<?php

defined( 'IN_CLASS' ) or die( 'Access Error!' );

class ModelPic extends base {
	
	public function getNewPic() {
		$sql = "select pix.*,memb.name as owner_name 
				from $this->tpix as pix,$this->tmember as memb where pix.owner=memb.id order by pix.id desc limit 1";
		$aP = model::dbuser()->get_one( $sql );
		return $aP;
	}
	
	public function getTopX( $x = 3 ) {
		$sql = "SELECT t1.*,t2.name owner_name FROM $this->tpix t1,$this->tmember t2
				WHERE t1.owner=t2.id ORDER BY t1.info_score DESC LIMIT $x";
		$topx = model::dbuser()->get_all( $sql );
		return is_array( $topx ) ? $topx : array();
	}
	
	public function ls( $limit ) {
		$sql = "SELECT SQL_CALC_FOUND_ROWS t1.*,t2.name owner_name FROM $this->tpix t1,$this->tmember t2
				WHERE t1.owner=t2.id ORDER BY time DESC {$limit}";
		$pics = model::dbuser()->get_all( $sql );
		return is_array( $pics ) ? $pics : array();
	}
	
	public function getused() {
		$sql = "SELECT count(1) count, SUM(size)/1024/1024 used FROM $this->tpix";
		$aUsed = model::dbuser()->get_one( $sql );
		return is_array( $aUsed ) ? $aUsed : array();
	}
	
	public function get( $pid ) {
		$sql = "SELECT * FROM $this->tpix WHERE id='{$pid}' LIMIT 1";
		$re = model::dbuser()->get_one( $sql );
		return is_array( $re ) ? $re : array();
	}
	
	public function del( $pid, $filename, $type ) { //删除一张图片
		$return = 0;
		
		$r1 = @unlink( DIR_DAT . "pic/$filename" . ( 1 == $type ? ".gif" : ".jpg" ) );
		$r2 = @unlink( DIR_DAT . "pic/{$filename}_s.jpg" );
		if( $r1 && $r2 ) {
			$sql = "DELETE FROM $this->tpix WHERE id='{$pid}' LIMIT 1";
			model::dbuser()->exe( $sql );
			$return += model::dbuser()->update_rows();
			$sql = "DELETE FROM $this->tcomment WHERE author='{$pid}' LIMIT 1";
			model::dbuser()->exe( $sql );
			$return += model::dbuser()->update_rows();
		}

		return $return;
	}
	
	public function add( $uid, $brief ) { //添加一张相片
		$return = 0;
		if( !file_exists( $_FILES['file']['tmp_name'] ) ) { //确保文件存在
			return $return;
		}

		$spic_max_width = 100; //缩徽图最大宽
		$spic_max_height = 75; //缩微图最大高

		$new_file_name = time(); //随机获取文件名
		$new_file_type = ( strstr( $_FILES['file']['type'] , 'gif' ) ? 1 : 0 );
		$new_file = DIR_DAT . "pic/$new_file_name" . ( $new_file_type ? '.gif' : '.jpg' ); //新文件名
		$new_sfile = DIR_DAT . "pic/{$new_file_name}_s.jpg";   //新缩微图名
		if ( move_uploaded_file( $_FILES['file']['tmp_name'] , $new_file ) ) { //将上传图片移到相应文件夹
			$new_file_info = getimagesize( $new_file );
			$tmp_hvw = $new_file_info[1] / $new_file_info[0];
			if ( $tmp_hvw > ( $spic_max_height / $spic_max_width ) ) {
				$spic_height = $spic_max_height;
				$spic_width = round( $spic_max_height / $tmp_hvw );
			} else {
				$spic_width = $spic_max_width;
				$spic_height = round( $tmp_hvw * $spic_max_width );
			}
			$im = ( $new_file_type ? imagecreatefromgif( $new_file ) : imagecreatefromjpeg( $new_file ) );
			$ni = imagecreatetruecolor( $spic_width , $spic_height );
			imagecopyresized( $ni , $im , 0 , 0 , 0 , 0, $spic_width , $spic_height , $new_file_info[0] , $new_file_info[1] );
			imagejpeg( $ni , $new_sfile ); //建缩略图

			$sql = "INSERT INTO $this->tpix SET filename={$new_file_name},
					owner='{$uid}',
					oldname='{$_FILES['file']['name']}',
					size={$_FILES['file']['size']},
					type={$new_file_type},
					time=NOW(),
					height={$new_file_info[1]},
					width={$new_file_info[0]},
					brief='{$brief}'";
			model::dbuser()->exe( $sql );
			$return = model::dbuser()->update_rows();
		}
		
		return $return;
	}
	
	public function getleftlist( $pid ) { //获得相册左侧的列表
		
		$sql = "SELECT t1.*,t2.name as owner_name FROM $this->tpix t1,$this->tmember t2
				WHERE t1.id>={$pid} AND t1.owner=t2.id ORDER BY id ASC LIMIT 4";
		$aBefore = model::dbuser()->get_all( $sql );//当前相片前4条(包括当前条)
		$aBefore = array_reverse( $aBefore );  //将前4条结果反序
		
		$sql = "SELECT t1.*,t2.name owner_name FROM $this->tpix t1,$this->tmember t2
				WHERE t1.id<{$pid} AND t1.owner=t2.id ORDER BY id DESC LIMIT 3";
		$aAfter = model::dbuser()->get_all( $sql );//当前相片后3条(不包括当前条)
		
		$pics = array_merge( $aBefore, $aAfter );//合并两次查询的结果
		$num = count ( $pics );
		
		$pic_max_width = 790; //图片最大宽度
		$curr_pic = array();
		foreach ( $pics as $i => $v ) {
			$pics[$i]['size'] = round ( $v['size'] / 1024 );
			$pics[$i]['sfilename'] = $v['filename'] . '_s.jpg';  //得到缩微图文件名
			$pics[$i]['filename'] = $v['filename'] . ( $v['type'] == 0 ? '.jpg' : '.gif' ); //大图的文件名
			$pics[$i]['width'] = min( $v['width'], $pic_max_width ); //指定图片最大宽度
			if ( $pics[$i]['id'] == $pid ) {
				$pics['curr'] = $pics[$i];
			}
		}
		
		return $pics;
	}
}